package com.changgou.seckill.mq;

import com.alibaba.fastjson.JSON;
import com.changgou.seckill.pojo.SeckillOrder;
import com.changgou.seckill.service.SeckillOrderService;
import entity.SeckillStatus;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 秒杀商品订单监听
 */
@Component
@RabbitListener(queues = "seckillQueue")
public class DelaySeckillMessageListener {
    @Autowired
    private SeckillOrderService seckillOrderService;
    @Autowired
    private RedisTemplate redisTemplate;
    @RabbitHandler
    public void getMessage(String message){
        //获取用户排队信息
        SeckillStatus seckillStatus = JSON.parseObject(message, SeckillStatus.class);
        //获取Redis中订单信息
        Object userQueueStatus = redisTemplate.boundHashOps("UserQueueStatus").get(seckillStatus.getUsername());
        if(userQueueStatus!=null){
            //关闭微信支付,删除订单
            seckillOrderService.closeOrder(seckillStatus.getUsername());
        }
    }
}
